package com.example.common.base

import android.view.View
import android.view.ViewGroup
import androidx.collection.SparseArrayCompat
import androidx.recyclerview.widget.RecyclerView

class HeaderAndFooterAdapter<T> : RecyclerView.Adapter<BaseViewHolder> {
    val BASE_ITEM_TYPE_HEADER = 100000
    val BASE_ITEM_TYPE_FOOTER = 200000
    private val mHeaderViews: SparseArrayCompat<View> = SparseArrayCompat()
    private val mFooterViews: SparseArrayCompat<View> = SparseArrayCompat()

    private var mInnerAdapter: BaseAdapter<T>? = null

    constructor (innerAdapter: BaseAdapter<T>) {
        mInnerAdapter = innerAdapter
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        if (mHeaderViews.get(viewType) != null) {
            return MyViewHolder(mHeaderViews.get(viewType))
        } else if (mFooterViews.get(viewType) != null) {
            return MyViewHolder(mFooterViews.get(viewType))
        }
        return mInnerAdapter!!.onCreateViewHolder(parent, viewType)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        if (isHeaderViewPos(position))
            return
        if (isFooterViewPos(position))
            return
        mInnerAdapter!!.onBindViewHolder(holder, position - getHeadersCount())
    }


    override fun getItemCount(): Int {
        return getHeadersCount() + getFootersCount() + getRealItemCount()
    }

    override fun getItemViewType(position: Int): Int {
        if (isHeaderViewPos(position)) {
            return mHeaderViews.keyAt(position)
        } else if (isFooterViewPos(position)) {
            return mFooterViews.keyAt(position - getHeadersCount() - getRealItemCount())
        }
        return mInnerAdapter!!.getItemViewType(position - getHeadersCount())
    }

    /**
     * 获取正常数据的size
     * @return
     */
    private fun getRealItemCount(): Int {
        return mInnerAdapter!!.itemCount
    }

    /**
     * 判断是否是Header
     * @param position
     * @return
     */
    private fun isHeaderViewPos(position: Int): Boolean {
        return position < getHeadersCount()
    }

    fun upDate(data: ArrayList<T>) {
        mInnerAdapter?.updateData(data)
        notifyDataSetChanged()
    }

    fun <B> upDataItem(position: Int, b: B) {
        mInnerAdapter?.upDataItem(position,b)
        notifyDataSetChanged()
    }

    fun deleteItem(position: Int){
        mInnerAdapter?.deleteItem(position)
        notifyDataSetChanged()
    }

    fun getCount():Int{
        return if (mInnerAdapter == null){
            0
        }else{
            mInnerAdapter!!.itemCount
        }
    }

    /**
     * 判断是否是Footer
     * @param position
     * @return
     */
    private fun isFooterViewPos(position: Int): Boolean {
        return position >= getHeadersCount() + getRealItemCount()
    }


    fun addHeaderView(view: View?) {
        mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view)
    }

    fun addFooterView(view: View?) {
        mFooterViews.put(mFooterViews.size() + BASE_ITEM_TYPE_FOOTER, view)
    }

    private fun getHeadersCount(): Int {
        return mHeaderViews.size()
    }

    fun getFootersCount(): Int {
        return mFooterViews.size()
    }

    internal class MyViewHolder(itemView: View?) : BaseViewHolder(itemView!!) {
        override fun initView(itemView: View) {
        }

    }

}

